package com.yuan.app.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yuan.app.pms.entity.AttrAttrgroupRelationEntity;
import com.yuan.app.pms.entity.AttrEntity;
import com.yuan.app.pms.entity.vo.AttrRelationDeleteVo;
import com.yuan.app.pms.service.AttrAttrgroupRelationService;
import com.yuan.app.pms.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.common.utils.PageUtils;
import com.yuan.common.utils.Query;

import com.yuan.app.pms.dao.AttrGroupDao;
import com.yuan.app.pms.entity.AttrGroupEntity;
import com.yuan.app.pms.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or()
                        .like("attr_group_name", key);
            });
        }


        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.and(e -> {
                e.eq("catelog_id", catelogId);
            });
            // wrapper.eq("catelog_id", catelogId);

            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    @Override
    public List<AttrEntity> relationList(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new UpdateWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", attrGroupId)
        );
        List<AttrEntity> list = new ArrayList<>();
        if (relationEntities != null) {
            relationEntities.stream().map(
                    (relation) -> {

                        AttrEntity byId = attrService.getById(relation.getAttrId());
                        list.add(byId);
                        return list;
                    }
            );
        }

        return list;
    }

    @Override
    public void removeRelation(AttrRelationDeleteVo[] relationDeleteVos) {

        List<AttrRelationDeleteVo> vos = Arrays.asList(relationDeleteVos);

        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((v) -> {
            AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
            relation.setAttrId(v.getAttrId());
            relation.setAttrGroupId(v.getAttrId());
            return relation;
        }).collect(Collectors.toList());

        baseMapper.deleteRElations(collect);

    }


}